<?php
/**
*
* @package RCB - R/C Booking
* @version $Id: book.php 10 2009-04-19 09:17:23Z scott $
* @copyright (c) 2008 Scott Deakin
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'rcb/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/rcb');

page_header($user->lang['RCB_TITLE']);

$template->set_filenames(array(
    'body' => 'rcb/book_body.html',
));

if(!$auth->acl_get('u_viewprofile')) 
{
	// this is not correct - we need to check the meeting
	// is open so that someone has not tried to circumvent
	// the meeting list logic
	trigger_error("You are not allowed to make or modify a booking");
}

/*************************************************************
 * get_booking_details()
 *
 * Generic function to gather all the details required to
 * populate the input areas on the page
 *************************************************************/

function get_booking_details($db, $template, $meeting_id, $user_id) 
{
	$sql = 'SELECT name, brca
			FROM ' . RCB_DRIVER_TABLE . '
			WHERE user_id = ' . (int) $user_id;
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);

	$template->assign_vars(array(
		'RCB_USER_ID' => $user_id,
		'RCB_NAME' => $row['name'],
		'RCB_BRCA' => $row['brca'],
		'MEETING_ID' => $meeting_id
	));

	// make a list of valid frequencies
	// maybe this should really go in a database...
	$freq_array = array('2.4');
	for($ff = 665; $ff <= 995; $ff+=10)
	{
	  $freq_array[] = '40.'.$ff;
	}
	array_push($freq_array, 'G/Br','Brown','R/Br',
							'Red','R/Or','Orange','O/Y','Yellow',
							'Y/Gr','Green','Bl/G','Blue');

	$sql = 'SELECT c.class_id, c.description, p.pt, p.freq, p.car, b.booked
			FROM '. RCB_CLASS_TABLE . ' AS c
			INNER JOIN ' . RCB_MEETING_CLASS_TABLE . ' AS m 
			ON c.class_id = m.class_id
			LEFT JOIN (
				SELECT class_id, 1 AS booked
				FROM ' . RCB_BOOKING_TABLE . '
				WHERE meeting_id = ' . (int) $meeting_id . '
					AND user_id = ' . (int) $user_id . '
			) AS b
			ON b.class_id = c.class_id
			LEFT JOIN ' . RCB_PROFILE_TABLE . ' AS p
			ON p.class_id = m.class_id
				AND p.user_id = ' . (int) $user_id . '
			WHERE m.meeting_id = ' . (int) $meeting_id . '
			ORDER BY c.class_id';

	$result = $db->sql_query($sql);

	for($class_count = 0; $row = $db->sql_fetchrow($result); $class_count++) 
	{
		$template->assign_block_vars('classrow', array(
			'S_CHECKED' => ($row['booked'] == 1 ? 'checked' : ''),
			'CLASS_ID' => $row['class_id'],
			'DESCRIPTION' => $row['description'],
			'CAR' => $row['car'],
			'PT' => ($row['pt'] == '0') ? '' : $row['pt']
		));
		// split the freq into at most 4 parts
		$freq = explode(" ", $row['freq']);
		for($fid = 0; $fid < 4; $fid++) 
		{
			$template->assign_block_vars('classrow.freq_id', array('FREQ_ID' => $fid));
			// loop through each freq and add it to the block variables
			foreach($freq_array as $c)
			{
				$template->assign_block_vars('classrow.freq_id.validfreq', array(
					'FREQ' => $c, 
					'S_SELECTED' => ($freq[$fid] == $c ? 'selected' : '')
				));
			}
		}
	}
	$template->assign_vars(array(
		'CLASS_COUNT' => $class_count
	));

}

/*************************************************************/
function update_driver_details($db, $user_id) 
{

	// first delete all current driver details
	$sql = 'DELETE FROM ' . RCB_DRIVER_TABLE . '
		WHERE user_id = ' . (int) $user_id;
	$db->sql_query($sql);
	
	// add the new details
	$sql_array = array(
		'user_id'      => $user_id,
		'name'      => request_var('name',''),
		'brca'      => (request_var('brca', 0) != 0) ? 
						request_var('brca', 0) : null	
	);
	$sql = 'INSERT INTO ' . RCB_DRIVER_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_array);
	$result = $db->sql_query($sql);
	
}
/*************************************************************/
function update_booking_details($db, $meeting_id, $user_id) 
{

	// first clear out all bookings for the user 
	// at this meeting

	$sql = 'DELETE FROM ' . RCB_BOOKING_TABLE . '
		WHERE user_id = ' . (int) $user_id . '
		AND meeting_id = ' . (int) $meeting_id;   
	$db->sql_query($sql);

	// now lets add the new booking details
	$booking_array = request_var('booked',array('' => ''));
	$class_id_array = request_var('class_id',array('' => 0));

	for($i = 0; $i < sizeof($class_id_array); $i++)
	{
		if($booking_array[$i] == "on")
		{ 
			$sql_array = array(
				'user_id' => $user_id,
				'meeting_id' => $meeting_id,
				'class_id' => $class_id_array[$i]
			);
			$sql = 'INSERT INTO ' . RCB_BOOKING_TABLE . ' ' . 
				$db->sql_build_array('INSERT', $sql_array);
			$result = $db->sql_query($sql);
					
		}
	}
	


}
/*************************************************************/
function update_profile_details($db, $user_id)
{
	$class_id_array = request_var('class_id',array('' => 0));
	$car_array = request_var('car',array('' => ''));
	$pt_array = request_var('pt', array('' => 0));

	$freq_array = request_var('freq', array('' => array('' => '')));

	// loop through each class updating the details
	for($i = 0; $i < sizeof($class_id_array); $i++)
	{
		// first clear out the class profile details
		$sql = 'DELETE FROM ' . RCB_PROFILE_TABLE . '
			WHERE user_id = ' . (int) $user_id . '
			AND class_id = ' . (int) $class_id_array[$i];
		$db->sql_query($sql);

		// join all the frequencies together
		$freq = join(" ", $freq_array[$i]);

		// and now add the new stuff
		$sql_array = array(
			'user_id' => $user_id,
			'class_id' => $class_id_array[$i],
			'pt' => $pt_array[$i],
			'freq' => $freq,
			'car' => $car_array[$i]
		);
		$sql = 'INSERT INTO ' . RCB_PROFILE_TABLE . ' ' . 
			$db->sql_build_array('INSERT', $sql_array);
		$result = $db->sql_query($sql);
		
	}
			
}
/*************************************************************/
$submit = request_var('submit','');
$meeting_id = request_var('meeting_id',0);

switch($submit)
{
    case 'Submit':
		update_driver_details($db, $user->data['user_id']);
		update_profile_details($db, $user->data['user_id']);
		update_booking_details($db, $meeting_id, $user->data['user_id']);
		redirect('index.php');
        break;
    case 'Cancel':
        redirect('index.php');
        break;
    default:
		// just get the variables and display them
		get_booking_details($db, $template, $meeting_id, $user->data['user_id']);
        break;
}

page_footer();
?>

